var express = require('express');
var router = express.Router();
let { patients, doctors, OPDS, Moneys } = require("../model/model")


router.get("/index", async (req, res) => {
    let page = Number(req.query.page) || 1 // 当前页码
    let limit = Number(req.query.limit) || 5   // 每页条数
    let skip = Number(page - 1) * limit   // 跳过多少页

    let data = await patients.find().skip(skip).limit(limit)   // 查询分页
    let num = await patients.countDocuments()  // 总条数
    let numpage = Math.ceil(num / limit) // 总页数

    // 按照 _id 过滤
    let id = req.query._id
    if (id) {
        data = data.filter((p) => p._id == id)
    }

    // 按照 card 过滤
    let card = req.query.card
    if (card) {
        data = data.filter((item) => item.Card.includes(card))
    }

    // 按照 number 过滤
    let numbers = req.query.number
    if (numbers) {
        data = data.filter((item) => item.Documents.includes(numbers))
    }

    // 按照 name 过滤
    let name = req.query.name
    if (name) {
        data = data.filter((item) => item.Name.includes(name))
    }

    // 按照 status 过滤
    let starts = req.query.starts
    if (starts) {
        data = data.filter((item) => item.status.includes(starts))
    }

    res.send({
        code: 200,
        data,
        num,
        numpage,
    })
})

router.get("/index_style", async (reg, res) => {
    let page = Number(reg.query.page) || 1 // 当前页码
    let limit = Number(reg.query.limit) || 5   // 每页条数
    let skip = Number(page - 1) * limit   // 跳过多少页


    let data = await doctors.find().skip(skip).limit(limit)   // 查询分页
    let num = await patients.countDocuments()  // 总条数
    let numpage = Math.ceil(num / limit) // 总页数

    let cards = reg.query.index
    if (cards) {
        data = data.filter((item) => {
            return item.Card.includes(cards)
        })
    }

    let names = reg.query.names
    if (names) {
        data = data.filter((item) => {
            return item.Name.includes(names)
        })
    }

    let nums = reg.query.nums
    if (nums) {
        data = data.filter((item) => {
            return item.Numbers.includes(nums)
        })
    }

    let status = reg.query.status
    if (status) {
        data = data.filter((item) => {
            return item.Status.includes(status)
        })
    }
    let shen = reg.query.shen
    if (shen) {
        data = data.filter((item) => {
            return item.Names.includes(shen)
        })
    }

    let id = reg.query._id
    if (id) {
        data = data.filter((p) => p._id == id)
        console.log(data)
    }

    res.send({
        code: 200,
        data,
        num,
        numpage,
    })
})



router.get("/index_in", async (reg, res) => {
    let page = Number(reg.query.page) || 1 // 当前页码
    let limit = Number(reg.query.limit) || 5   // 每页条数
    let skip = Number(page - 1) * limit   // 跳过多少页


    let data = await OPDS.find().skip(skip).limit(limit)   // 查询分页
    let num = await patients.countDocuments()  // 总条数
    let numpage = Math.ceil(num / limit)
    let chufang = reg.query.chufang
    if (chufang) {
        data = data.filter((item) => {
            return item.chunum.includes(chufang)
        })
    }
    let name = reg.query.name
    if (name) {
        data = data.filter((item) => {
            return item.name.includes(name)
        })
    }

    let document = reg.query.document
    if (document) {
        data = data.filter((item) = {

        })
    }
    let menzheng = reg.query.zheng
    if (menzheng) {
        data = data.filter((item) => {
            return item.opd.includes(menzheng)
        })
    }
    let status = reg.query.status
    if (status) {
        data = data.filter((item) => {
            return item.status.includes(status)
        })
    }
    let doctor = reg.query.docuto
    if (doctor) {
        data = data.filter((item) => {
            return item.doctor.includes(doctor)
        })
    }

    let billing = reg.query.billing
    if (billing) {
        data = data.filter((item) => {
            return item.room.includes(billing)
        })
    }
    res.send({
        code: 200,
        data,
        num,
        numpage,
    })
})

router.get("/index_doctor", async (reg, res) => {
    let data = await Moneys.find()
    res.send({
        code: 200,
        data
    })
})




module.exports = router;